package com.lovo.sys.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    @Autowired
    CachingConnectionFactory connectionFactory;

    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory);
    }


    //创建审核交换机
    @Bean
    public DirectExchange directOrderExchange() {
        return new DirectExchange("SSc_Sys_Dev");
    }

    //创建采购订单交换机
    @Bean
    public DirectExchange directSchemeExchange() {
        return new DirectExchange("directSchemeExchange");
    }

    //创建促销订单交换机
    @Bean
    public DirectExchange directPromotionalExchange() {
        return new DirectExchange("directPromotionalExchange");
    }

    //创建上架审核状态
    @Bean
    public Queue queueAudit() {
        return new Queue("queueAudit");
    }

    //创建采购订单queue
    @Bean
    public Queue queueSheme() {
        return new Queue("queueSheme");
    }

    //创建促销订单queue
    @Bean
    public Queue queuePromotional() {
        return new Queue("queuePromotional");
    }

    //创建冻结队列
    @Bean
    public Queue frozenQueueTwo() {
        return new Queue("frozenQueueTwo");
    }


    //审核交换机和上架审核状态队列进行绑定
    @Bean
    public Binding directOrderExchangeToQueueOrder(@Qualifier("directOrderExchange") DirectExchange SSc_Sys_Dev, Queue queueAudit) {
        return BindingBuilder.bind(queueAudit).to(SSc_Sys_Dev).with("rabbit.audit");
    }
    @Bean
    public Binding directOrderExchangeToFrozenQueueTwo(@Qualifier("directOrderExchange") DirectExchange SSc_Sys_Dev, Queue frozenQueueTwo) {
        return BindingBuilder.bind(frozenQueueTwo).to(SSc_Sys_Dev).with("rabbit.frozen");
    }


    @Bean
    public Binding directSchemeExchangeToQueueSheme(DirectExchange directSchemeExchange, Queue queueSheme) {
        return BindingBuilder.bind(queueSheme).to(directSchemeExchange).with("rabbit.sheme");
    }

    @Bean
    public Binding directPromotionalExchangeToQueuePromotional(DirectExchange directPromotionalExchange, Queue queuePromotional) {
        return BindingBuilder.bind(queuePromotional).to(directPromotionalExchange).with("rabbit.promotional");
    }

}